/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月30日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr <br>
 */
@Service
public class LocalIPProtocolVerHandler extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    /**
     * Description: <br>
     *
     * @author tengg<br>
     * @taskId <br>
     * @param commonAttr <br>
     */
    @Override
    public void init(CommonAttrVO commonAttr) {
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        super.apply(sceneInstId, fieldName, value, valueDesc);
        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        //查询这个offer下有效的接入产品
        List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
        prodInstList.forEach(prodInst ->{
            KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "2_0000047");
            KiteAttr IPv4IPChageTypeProdAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "210004178");
            KiteAttr IPv6IPChageTypeProdAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "210004179");
            if (Objects.nonNull(prodAttr)) {
                if(KiteStringUtils.equalsAny(prodAttr.getValue(),"1_0000219" )){
                    //IPv4IPChageTypeProdAttr.setShowType("D");
                    if(Objects.nonNull(IPv4IPChageTypeProdAttr) && KiteStringUtils.isEmpty(IPv4IPChageTypeProdAttr.getValue())){
                        IPv4IPChageTypeProdAttr.setValue("100007396");
                    }
                    // IPv6IPChageTypeProdAttr.setValue("100007396");
                    //operateInstAttrService.setAttrValue(prodInst, "210004178", IPv4IPChageTypeProdAttr.getValue(), sceneInstId, null);
                } else if(KiteStringUtils.equalsAny(prodAttr.getValue(),"1_0000220")) {
                    //IPv6IPChageTypeProdAttr.setShowType("D");
                    if(Objects.nonNull(IPv6IPChageTypeProdAttr) && KiteStringUtils.isEmpty(IPv6IPChageTypeProdAttr.getValue())){
                        IPv6IPChageTypeProdAttr.setValue("100007396");
                    }
                    //IPv4IPChageTypeProdAttr.setValue("100007396");
                    //operateInstAttrService.setAttrValue(prodInst, "210004179", IPv6IPChageTypeProdAttr.getValue(), sceneInstId, null);
                }else if(KiteStringUtils.equalsAny(prodAttr.getValue(),"1_0000221")){
                    //IPv6IPChageTypeProdAttr.setShowType("D");
                    //IPv4IPChageTypeProdAttr.setShowType("D");
                    if( Objects.nonNull(IPv6IPChageTypeProdAttr) && Objects.nonNull(IPv4IPChageTypeProdAttr) && KiteStringUtils.isEmpty(IPv4IPChageTypeProdAttr.getValue()) && KiteStringUtils.isEmpty(IPv6IPChageTypeProdAttr.getValue())){
                        IPv4IPChageTypeProdAttr.setValue("100007396");
                        IPv6IPChageTypeProdAttr.setValue("100007396");
                    }
                   // operateInstAttrService.setAttrValue(prodInst, "210004178", IPv4IPChageTypeProdAttr.getValue(), sceneInstId, null);
                   // operateInstAttrService.setAttrValue(prodInst, "210004179", IPv6IPChageTypeProdAttr.getValue(), sceneInstId, null);
                }
                operateInstAttrService.setAttrValue(prodInst, prodAttr.getFieldName(), prodAttr.getValue(), sceneInstId, null);
            }
        });
        return ErrorConsts.SUCCESS.getResult(true);
    }
}
